import axios from 'axios';

axios.defaults.baseURL = '';

const request = {
    /**
     * POST 请求
     * @param url 接口地址
     * @param data 请求参数
     * @param config 请求配置
     * @returns {Promise<unknown>} 结果
     */
    post: (url, data = {}, config = {}) => {
        return new Promise((resolve, reject) => {
            axios({method: 'post', url: url, data: data, ...config}).then((response) => {
                resolve(response.data);
            }).catch((error) => {
                reject(error);
            });
        });
    },

    /**
     * DELETE 请求
     * @param url 接口地址
     * @param data 请求参数
     * @param config 请求配置
     * @returns {Promise<unknown>} 结果
     */
    delete: (url, data = {}, config = {}) => {
        return new Promise((resolve, reject) => {
            axios({method: 'delete', url: url, data: data, ...config}).then((response) => {
                resolve(response.data);
            }).catch((error) => {
                reject(error);
            });
        });
    },

    /**
     * PUT 请求
     * @param url 接口地址
     * @param data 请求参数
     * @param config 请求配置
     * @returns {Promise<unknown>} 结果
     */
    put: (url, data = {}, config = {}) => {
        return new Promise((resolve, reject) => {
            axios({method: 'put', url: url, data: data, ...config}).then((response) => {
                resolve(response.data);
            }).catch((error) => {
                reject(error);
            });
        });
    },

    /**
     * GET 请求
     * @param url 接口地址
     * @param params 请求参数
     * @param config 请求配置
     * @returns {Promise<unknown>} 结果
     */
    get: (url, params = {}, config = {}) => {
        return new Promise((resolve, reject) => {
            axios({method: 'get', url: url, params: params, ...config}).then((response) => {
                resolve(response.data);
            }).catch((error) => {
                reject(error);
            });
        });
    },

};

export default request;
